package com.glela.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class SessionUtils
{

	/**
	 * session中保存用户id的
	 */
	public static final String USER_ID = "user_id";
	public static final String USERINFO = "user_info";
	public static final String LOGIN_ACCOUNT = "login_account";
	public static final String OPEN_ID = "open_id";
	public static final String ACCESS_CODE = "ACCESS_CODE";
	public static final String SESSIONKEY = "sessionKey";
	public static final String LOGINSESSION = "loginSession";
	public static final int TIME = 15 * 60 * 60 * 60;

	public static void cleanUserIdFromSession()
	{
		SessionUtils.setSessionAttribute(USER_ID, null);
	}

	public static Object getAccountInfo()
	{
		HttpSession instance = SessionUtils.getSession();
		Object account = instance.getAttribute(LOGIN_ACCOUNT);
		return account;
	}

	//獲取登陸賬號信息
	public static LoginSession getLoginSession()
	{
		HttpSession instance = SessionUtils.getSession();
		LoginSession loginSession = (LoginSession) instance.getAttribute(LOGINSESSION);
		return loginSession;
	}

	public static Object getSessionAttribute(String attKey)
	{
		HttpSession instance = SessionUtils.getSession();
		return instance.getAttribute(attKey);
	}

	/**
	 * CMS登陆使用
	 * 
	 * @return
	 */
	public static Integer getUserIdFromSession()
	{
		Object obj = SessionUtils.getSessionAttribute(USER_ID);
		if (obj != null) {
			return Integer.valueOf(obj.toString());
		}
		return null;
	}

	public static Object getUserInfo()
	{
		HttpSession instance = SessionUtils.getSession();
		return instance.getAttribute(USERINFO);
	}

	public static void removeLoginSession()
	{
		HttpSession instance = SessionUtils.getSession();
		instance.removeAttribute(LOGIN_ACCOUNT);
		instance.removeAttribute(LOGINSESSION);
		instance.removeAttribute(USERINFO);
	}

	/**
	 * @return
	 */
	public static void saveAccessCode(String accessCode)
	{
		HttpSession instance = SessionUtils.getSession();
		instance.setAttribute(ACCESS_CODE, accessCode);
	}

	public static void saveAccountInfo(Object account, String userName, long userId, String passWord)
	{
		HttpSession instance = SessionUtils.getSession();
		instance.setAttribute(LOGIN_ACCOUNT, account);
		String SessionKey = MD5Utils.encrypt(userName + passWord);
		LoginSession loginSession = new LoginSession();
		loginSession.setUserId(userId);
		loginSession.setSessionKey(SessionKey);
		loginSession.setUserName(userName);
		SessionUtils.setSessionSessionKey(LOGINSESSION, loginSession);
	}
	
	public static void saveAccountInfo(HttpSession instance, Object account, String userName, long userId, String passWord)
	{
		if(instance == null) instance = SessionUtils.getSession();
		instance.setAttribute(LOGIN_ACCOUNT, account);
		String SessionKey = MD5Utils.encrypt(userName + passWord);
		LoginSession loginSession = new LoginSession();
		loginSession.setUserId(userId);
		loginSession.setSessionKey(SessionKey);
		loginSession.setUserName(userName);
		instance.setMaxInactiveInterval(TIME);
		instance.setAttribute(LOGINSESSION, loginSession);
	}

	public static void saveUserIdToSession(Integer userId)
	{
		SessionUtils.setSessionAttribute(USER_ID, userId);
	}

	public static void saveUserInfo(Object account)
	{
		HttpSession instance = SessionUtils.getSession();
		instance.setAttribute(USERINFO, account);
	}

	/**
	 * @param attKey
	 * @param attVal
	 */
	public static void setSessionAttribute(String attKey, Object attVal)
	{
		HttpSession instance = SessionUtils.getSession();
		instance.setAttribute(attKey, attVal);
	}

	/**
	 * @param attKey
	 * @param attVal
	 */
	public static void setSessionSessionKey(String attKey, LoginSession loginSession)
	{
		HttpSession instance = SessionUtils.getSession();
		instance.setAttribute(attKey, loginSession);
	}

	public static void setTimeOut(int timeOut)
	{
		HttpSession instance = SessionUtils.getSession();
		instance.setMaxInactiveInterval(timeOut);
	}

	private static HttpServletRequest getRequest()
	{
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest();
	}

	private static HttpSession getSession()
	{
		HttpSession instance = SessionUtils.getRequest().getSession();
		instance.setMaxInactiveInterval(TIME);
		return instance;
	}

}